package com.youxin.busfoundation.common.es;

import com.youxin.busfoundation.common.GlobalConfig;
import com.youxin.busfoundation.common.es.bean.*;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import com.youxin.busfoundation.config.EsConfig;
import com.youxin.busfoundation.utils.es.EsBasePage;
import com.youxin.foundation.interfaces.Convert;
import com.youxin.foundation.utils.StringUtils;
import org.elasticsearch.index.query.TermQueryBuilder;

import java.util.List;

/**
 * 搜索线索
 * @author yingde.cao
 *
 */
public class SearchResource<T> extends BaseSearch<T>{
	
	public SearchResource(Convert<T> convert) {
		super(convert);
	}
	
	public SearchResource(){
		super();
	}
	
	@SuppressWarnings("unchecked")
	public EsBasePage<Object> queryList(RequestQueryLeadsBean bean,Class<T> cls) throws Exception {
		try {		
			String keywords = org.apache.commons.lang3.StringUtils.trim(bean.getKeywords());
			BoolQueryBuilder queryBuilder = null;
			if (!StringUtils.isNullOrEmpty(keywords)) {
				queryBuilder = new BoolQueryBuilder()
						.should(QueryBuilders.multiMatchQuery(keywords, "company_name", "title", "detail_text"));
				
				
				//srb.setQuery(queryBuilder);
				// srb.setFetchSource(includes, excludes)
				// HighlightBuilder hb = new HighlightBuilder();
				// hb.field("title").field("detail_text").field("company_name").preTags("...").postTags("...");
				// srb.highlighter(hb);
			}
			BoolQueryBuilder classifyQueryBuilder=addClassify(bean);
			if (classifyQueryBuilder != null) {		
				if (queryBuilder == null) {
					queryBuilder = new BoolQueryBuilder();
				}
				queryBuilder.must(classifyQueryBuilder);
			}
			return queryList(bean, cls, queryBuilder,
					(srb)->{
						String[] fields=new String[]
								{
								"uid","title","type","company_uid","company_name"
								,"short_name","supply_needs","industry","industry_small"
								,"member_name","member_position","member_uid","begin_time","end_time","area"};
						srb.setFetchSource(fields, null);
					}
					,new String[]{ EsConfig.getConfig().getResourcename()},null);
			
		} catch (Exception e) {
			throw e;
		} finally {			

		}

	}
	//ResourceBean.class
	@SuppressWarnings("unchecked")
	public List<T> query(RequestResourceBean requestResourceBean,Class<T> cls) throws Exception {
		try {
			TermQueryBuilder queryBuilder=QueryBuilders.termQuery("company_uid", requestResourceBean.getCompanyUid());
			return queryList(cls, queryBuilder, (srb)->{

			}, EsConfig.getConfig().getResourcename());

		} catch (Exception e) {
			throw e;
		} finally {

		}
	}

	/** 
	 * @desc PortraitResourceBean.class
	 * @desc 查询ES资源
	 * @param requestBean
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	public List<T> queryResource(RequestResourceBean requestBean,Class<T> cls) throws Exception {
		try {
			TermQueryBuilder queryBuilder=QueryBuilders.termQuery("company_uid", requestBean.getCompanyUid());
			return queryList(cls, queryBuilder, (srb)->{

			}, EsConfig.getConfig().getResourcename());

		} catch (Exception e) {
			throw e;
		} finally {

		}
	}
	
}
